package com.example.time;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;
import java.util.concurrent.TimeUnit;

//002
/*
 * LocalDate、LocalTime、LocalDateTime
 * Instant:时间戳（以Unix时间元年到此时之间的毫秒值）
 * Duration:计算两个时间之间的间隔
 * Period:计算两个日期之间的间隔
 * TemporalAdjuster:时间矫正器。有时我们可能需要进行将日期调整到“下个周日”等操作。
 * TemporalAdjusters:该类通过静态方法提供了大量的常用TemporalAdjuster的实现。
 * DataTimeFormatter:格式化时间日期
 * ZonedDate、ZonedTime、ZonedDateTime:时区操作
 */
public class TestLocalDateTime {
    @Test
    public void test1() {
        //获取当前时间
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);

        LocalDateTime ldt2 = LocalDateTime.of(2020, 9, 23, 17, 34, 00);
        System.out.println(ldt2);

        //运算完返回新对象
        LocalDateTime ldt3 = ldt.plusYears(1);
        System.out.println(ldt3);

        LocalDateTime ldt4 = ldt.minusMonths(2);
        System.out.println(ldt4);

        System.out.println(ldt.getYear());
        System.out.println(ldt.getMonthValue());
        System.out.println(ldt.getDayOfMonth());
        System.out.println(ldt.getHour());
        System.out.println(ldt.getMinute());
    }

    @Test
    public void test2() {
        //默认获取UTC时区
        Instant now = Instant.now();
        System.out.println(now);

        //添加偏移量
        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);

        //转毫秒
        System.out.println(now.toEpochMilli());

        //操作时间戳
        Instant ofEpochSecond = Instant.ofEpochSecond(1000);
        System.out.println(ofEpochSecond);
    }

    //Duration
    @Test
    public void test3() throws InterruptedException {
        Instant start1 = Instant.now();
        TimeUnit.SECONDS.sleep(2);
        Instant end1 = Instant.now();
        Duration between1 = Duration.between(start1, end1);
        System.out.println(between1.toMillis());

        LocalDateTime start2 = LocalDateTime.now();
        TimeUnit.SECONDS.sleep(2);
        LocalDateTime end2 = LocalDateTime.now();
        Duration between2 = Duration.between(start2, end2);
        System.out.println(between2.toMillis());

    }

    //Period
    @Test
    public void test4() {
        LocalDate start = LocalDate.now();
        LocalDate end = LocalDate.of(2019, 10, 15);

        Period between = Period.between(start, end);
        System.out.println(between.getYears());
        System.out.println(between.getMonths());
        System.out.println(between.getDays());
    }

    //TemporalAdjuster
    @Test
    public void test5() {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        //下一个25日
        LocalDateTime dateTime = now.withDayOfMonth(25);
        System.out.println(dateTime);

        //以一个周日
        LocalDateTime nextSun = now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        System.out.println(nextSun);

        //自定义：下一个工作日
        LocalDateTime nextWorkDay = now.with((l) -> {
            LocalDateTime dt = (LocalDateTime) l;
            DayOfWeek dayOfWeek = dt.getDayOfWeek();
            if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
                return dt.plusDays(3);
            } else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
                return dt.plusDays(2);
            } else {
                return dt.plusDays(1);
            }
        });
        System.out.println(nextWorkDay);
    }

    @Test
    public void test6() {
        //DataTimeFormatter:格式化时间日期
        DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME;
        LocalDateTime ldt = LocalDateTime.now();
        String strDate = ldt.format(dtf);
        System.out.println(strDate);

        System.out.println("--------------------------");
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String strDate2 = dtf2.format(ldt);
        System.out.println(strDate2);
        System.out.println("--------------------------");
        LocalDateTime newDate = LocalDateTime.parse(strDate2, dtf2);
        System.out.println(newDate);
    }

    @Test
    public void test7() {
        //ZonedDate、ZonedTime、ZonedDateTime:时区操作
        Set<String> zoneIds = ZoneId.getAvailableZoneIds();
        zoneIds.forEach(System.out::println);
        zoneIds.stream().filter((s) -> s.contains("Shang")).forEach(System.out::println);
    }

    @Test
    public void test8() {
        LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Europe/London"));
        System.out.println(ldt);

        LocalDateTime ldt2 = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        ZonedDateTime zonedDateTime = ldt2.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zonedDateTime);
    }
}
